# cmake_minimum_required(VERSION 3.0.2)
cmake_minimum_required(VERSION 3.13)

project(know_node)

set(CMAKE_CXX_STANDARD 20)


# =========================================================== #
#             User Config Start                               #
# =========================================================== #
# 设置交叉编译工具链
# set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
# set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)

# 打印工具链信息
message(STATUS "Using C compiler: ${CMAKE_C_COMPILER}")
message(STATUS "Using C++ compiler: ${CMAKE_CXX_COMPILER}")

# 查找 src 目录及其子目录下的所有 .c 和 .cpp 文件
file(GLOB_RECURSE SRC_SOURCES 
    "${CMAKE_CURRENT_SOURCE_DIR}/src/*.c" 
    "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
)

# 查找 src/test 目录下的所有 .c 和 .cpp 文件
file(GLOB_RECURSE TEST_SOURCES 
    "${CMAKE_CURRENT_SOURCE_DIR}/src/test/*.c" 
    "${CMAKE_CURRENT_SOURCE_DIR}/src/test/*.cpp"
)

# 查找 src/tools 目录下的所有 .c 和 .cpp 文件
file(GLOB_RECURSE TOOLS_SOURCES 
    "${CMAKE_CURRENT_SOURCE_DIR}/src/tools/*.c" 
    "${CMAKE_CURRENT_SOURCE_DIR}/src/tools/*.cpp"
)

# 查找 src/node 目录下的所有 .c 和 .cpp 文件
file(GLOB_RECURSE NODE_SOURCES 
    "${CMAKE_CURRENT_SOURCE_DIR}/src/node/*.c" 
    "${CMAKE_CURRENT_SOURCE_DIR}/src/node/*.cpp"
)

# 查找 src/nodelet 目录下的所有 .c 和 .cpp 文件
file(GLOB_RECURSE NODELET_SOURCES 
    "${CMAKE_CURRENT_SOURCE_DIR}/src/nodelet/*.c" 
    "${CMAKE_CURRENT_SOURCE_DIR}/src/nodelet/*.cpp"
)

# 从 SRC_SOURCES 中移除 TEST_SOURCES
list(REMOVE_ITEM SRC_SOURCES 
    ${TEST_SOURCES} ${TOOLS_SOURCES} ${NODE_SOURCES} ${NODELET_SOURCES})


set(BASE_CPP_PATH ${SRC_SOURCES})
message(STATUS "BASE_CPP_PATH files: ${BASE_CPP_PATH}")



# 添加 pkg-config 的支持 (pkg_check_modules 是 CMake 对 pkg-config 的简单封装)
# 启用 pkg-config 查找
find_package(PkgConfig REQUIRED)

# 打印 pkg-config 检索的信息
message(STATUS "PkgConfig found")

find_package(pybind11 REQUIRED)

find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
# find_package(Python3 REQUIRED)


message(STATUS "Python3_EXECUTABLE: ${Python3_EXECUTABLE}")
message(STATUS "Python3_INCLUDE_DIR: ${Python3_INCLUDE_DIR}")
message(STATUS "Python3_LIBRARY: ${Python3_LIBRARY}")


# 查找库的头文件标志和链接标志
pkg_check_modules(SHERPA_ONNX REQUIRED sherpa-onnx)
pkg_check_modules(LIBCURL REQUIRED libcurl)
pkg_check_modules(PORTAUDIO REQUIRED portaudio-2.0)
pkg_check_modules(ICE REQUIRED icu-uc)
pkg_check_modules(SPEEXDSP REQUIRED speexdsp)
pkg_check_modules(LIBSAMPLERATE REQUIRED samplerate)
pkg_check_modules(WEBSRTC_AUDIO_PROCESSING REQUIRED webrtc-audio-processing)

# =========================================================== #
#             User Config End                                 #
# =========================================================== #



## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  message_generation
)

## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)


## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()

################################################
## Declare ROS messages, services and actions ##
################################################

## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
##   your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
##   * add a build_depend tag for "message_generation"
##   * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
##   * If MSG_DEP_SET isn't empty the following dependency has been pulled in
##     but can be declared for certainty nonetheless:
##     * add a exec_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
##   * add "message_generation" and every package in MSG_DEP_SET to
##     find_package(catkin REQUIRED COMPONENTS ...)
##   * add "message_runtime" and every package in MSG_DEP_SET to
##     catkin_package(CATKIN_DEPENDS ...)
##   * uncomment the add_*_files sections below as needed
##     and list every .msg/.srv/.action file to be processed
##   * uncomment the generate_messages entry below
##   * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)

## Generate messages in the 'msg' folder
# add_message_files(
#   FILES
#   Message1.msg
#   Message2.msg
# )

## Generate services in the 'srv' folder
add_service_files(
  FILES
  people_id_lan.srv
  llm_asr.srv
  llm_srv.srv
  llm_tts.srv
  llm_goBack.srv
)

## Generate actions in the 'action' folder
# add_action_files(
#   FILES
#   Action1.action
#   Action2.action
# )

## Generate added messages and services with any dependencies listed here
generate_messages(
  DEPENDENCIES
  std_msgs
)

################################################
## Declare ROS dynamic reconfigure parameters ##
################################################

## To declare and build dynamic reconfigure parameters within this
## package, follow these steps:
## * In the file package.xml:
##   * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
## * In this file (CMakeLists.txt):
##   * add "dynamic_reconfigure" to
##     find_package(catkin REQUIRED COMPONENTS ...)
##   * uncomment the "generate_dynamic_reconfigure_options" section below
##     and list every .cfg file to be processed

## Generate dynamic reconfigure parameters in the 'cfg' folder
# generate_dynamic_reconfigure_options(
#   cfg/DynReconf1.cfg
#   cfg/DynReconf2.cfg
# )

###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES llm_node
#  CATKIN_DEPENDS roscpp rospy std_msgs
#  DEPENDS system_lib
CATKIN_DEPENDS message_runtime
)

###########
## Build ##
###########

## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
    ${catkin_INCLUDE_DIRS}
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    ${CMAKE_CURRENT_SOURCE_DIR}/include/asr
    ${CMAKE_CURRENT_SOURCE_DIR}/include/audio
    ${CMAKE_CURRENT_SOURCE_DIR}/include/llm
    ${CMAKE_CURRENT_SOURCE_DIR}/include/utils
    ${CMAKE_CURRENT_SOURCE_DIR}/include/test
    ${CMAKE_CURRENT_SOURCE_DIR}/include/keywords
    ${CMAKE_CURRENT_SOURCE_DIR}/include/huzhou
    ${CMAKE_CURRENT_SOURCE_DIR}/include/medical 

    ${SHERPA_ONNX_INCLUDE_DIRS}
    ${LIBCURL_INCLUDE_DIRS}
    ${PORTAUDIO_INCLUDE_DIRS}
    ${ICE_INCLUDE_DIRS}
    ${SPEEXDSP_INCLUDE_DIRS}
    ${LIBSAMPLERATE_INCLUDE_DIRS}
)
#     ${WEBSRTC_AUDIO_PROCESSING_INCLUDE_DIRS}
# )
#     /usr/local/include/liquid/
# )

# 打印生成的可执行文件
# message(STATUS "Executable file: ${EXECUTABLE}")

# 设置链接库的路径变量
set(LINK_DIRECTORIES
    ${SHERPA_ONNX_LIBRARY_DIRS}
    ${LIBCURL_LIBRARY_DIRS}
    ${PORTAUDIO_LIBRARY_DIRS}
    ${ICE_LIBRARY_DIRS}
    ${SPEEXDSP_LIBRARY_DIRS}
    ${LIBSAMPLERATE_LIBRARY_DIRS}
)
#     ${WEBSRTC_AUDIO_PROCESSING_LIBRARY_DIRS}
# )
#     /usr/local/lib/
# )

# 设置链接库的库列表变量
set(LINK_LIBRARIES
    ${SHERPA_ONNX_LIBRARIES}
    ${LIBCURL_LIBRARIES}
    ${PORTAUDIO_LIBRARIES}
    ${ICE_LIBRARIES}
    ${SPEEXDSP_LIBRARIES}
    ${LIBSAMPLERATE_LIBRARIES}

    pybind11::module

    Python3::Python
    
    ${catkin_LIBRARIES}
)
# ${WEBSRTC_AUDIO_PROCESSING_LIBRARIES}
#     liquid
# )

set(COM_CFLAGS_OPT
    ${SPEEXDSP_CFLAGS_OTHER}
)


# =========================================================== #
#  ROS NODE >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>   #
# =========================================================== #

# 定义一个函数来封装可执行文件的添加过程
function(add_ros_node NODE_NAME MAIN_PATH)
    add_executable(${NODE_NAME} ${BASE_CPP_PATH} ${MAIN_PATH})

    # 设置链接库的链接路径
    target_link_directories(${NODE_NAME} PRIVATE ${LINK_DIRECTORIES})

    # 设置链接库的链接路径和链接标识
    target_link_libraries(${NODE_NAME} ${LINK_LIBRARIES})

    # 设置编译选项
    target_compile_options(${NODE_NAME} PRIVATE ${COM_CFLAGS_OPT})

    # 添加依赖项
    add_dependencies(${NODE_NAME} ${catkin_EXPORTED_TARGETS})
endfunction()


# add_ros_node(llm_node "${CMAKE_CURRENT_SOURCE_DIR}/src/node/medical_main.cpp")

add_ros_node(know_asr_tts_node "${CMAKE_CURRENT_SOURCE_DIR}/src/node/know_asr_tts_node.cpp")

# add_ros_node(llm_server "${CMAKE_CURRENT_SOURCE_DIR}/src/node/medical_llm.cpp")

# add_ros_node(llm_dialog "${CMAKE_CURRENT_SOURCE_DIR}/src/node/medical_dialog.cpp")

# add_ros_node(llm_kws "${CMAKE_CURRENT_SOURCE_DIR}/src/node/medical_kws.cpp")

# add_ros_node(llm_core "${CMAKE_CURRENT_SOURCE_DIR}/src/node/medical_core.cpp")

# =========================================================== #
#  ROS NODE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  #
# =========================================================== #



# =========================================================== #
# Sherpa Test Start    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>   #
# =========================================================== #
# 定义一个创建测试可执行文件的函数 params: [1] 可执行文件的名称， [2] 测试文件的名称
function(add_test_executable TEST_NAME SOURCE_FILE)
    add_executable(${TEST_NAME} 
        ${BASE_CPP_PATH} 
        "${CMAKE_CURRENT_SOURCE_DIR}/src/test/${SOURCE_FILE}"
    )
    target_link_directories(${TEST_NAME} PRIVATE ${LINK_DIRECTORIES})
    target_link_libraries(${TEST_NAME} ${LINK_LIBRARIES})
    target_compile_options(${TEST_NAME} PRIVATE ${COM_CFLAGS_OPT})
endfunction()

# 使用函数创建各个测试可执行文件
add_test_executable(ASR_TEST asr_test.cpp)
# add_test_executable(ASR_LLM_TTS_TEST asr_llm_tts_test.cpp)
add_test_executable(LLM_TEST llm_test.cpp)
add_test_executable(TTS_BTM_TEST tts_btm_test.cpp)
add_test_executable(TTS_TEST tts_test.cpp)
add_test_executable(PEOPLE_ID_TEST people_id_file_test.cpp)
# add_test_executable(ASR_LLM_TEST asr_llm_test.cpp)
add_test_executable(KWS_TEST kws_test.cpp)
# =========================================================== #
# Sherpa Test End     <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<    #
# =========================================================== #





# =========================================================== #
#                        TOOLS Start  >>>>>>>>>>>>>>>>>>>>    #
# =========================================================== #
set(GET_INPUT_DEV getInputDev)

add_executable(${GET_INPUT_DEV} ${BASE_CPP_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/src/tools/get_input_device.cpp")
target_link_directories(${GET_INPUT_DEV} PRIVATE ${LINK_DIRECTORIES})
target_link_libraries(${GET_INPUT_DEV} ${LINK_LIBRARIES})
target_compile_options(${GET_INPUT_DEV} PRIVATE ${COM_CFLAGS_OPT})
add_dependencies(${GET_INPUT_DEV} ${catkin_EXPORTED_TARGETS})


set(GET_OUTPUT_DEV getOutputDev)

add_executable(${GET_OUTPUT_DEV} ${BASE_CPP_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/src/tools/get_output_device.cpp")
target_link_directories(${GET_OUTPUT_DEV} PRIVATE ${LINK_DIRECTORIES})
target_link_libraries(${GET_OUTPUT_DEV} ${LINK_LIBRARIES})
target_compile_options(${GET_OUTPUT_DEV} PRIVATE ${COM_CFLAGS_OPT})
add_dependencies(${GET_OUTPUT_DEV} ${catkin_EXPORTED_TARGETS})
# =========================================================== #
#                        TOOLS END    <<<<<<<<<<<<<<<<<<<<<   #
# =========================================================== #



## Rename C++ executable without prefix
## The above recommended prefix causes long target names, the following renames the
## target back to the shorter version for ease of user use
## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")

## Add cmake target dependencies of the executable
## same as for the library above
# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

#############
## Install ##
#############

# all install targets should use catkin DESTINATION variables
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html

## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
catkin_install_python(PROGRAMS
  scripts/know_llm_node.py
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

## Mark executables for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_executables.html
# install(TARGETS ${PROJECT_NAME}_node
#   RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )

## Mark libraries for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_libraries.html
# install(TARGETS ${PROJECT_NAME}
#   ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
#   LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
#   RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
# )

## Mark cpp header files for installation
# install(DIRECTORY include/${PROJECT_NAME}/
#   DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
#   FILES_MATCHING PATTERN "*.h"
#   PATTERN ".svn" EXCLUDE
# )

## Mark other files for installation (e.g. launch and bag files, etc.)
# install(FILES
#   # myfile1
#   # myfile2
#   DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
# )

#############
## Testing ##
#############

## Add gtest based cpp test target and link libraries
# catkin_add_gtest(${PROJECT_NAME}-test test/test_llm_node.cpp)
# if(TARGET ${PROJECT_NAME}-test)
#   target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
# endif()

## Add folders to be run by python nosetests
# catkin_add_nosetests(test)
